<?php
//dezend by http://www.yunlu99.com/ QQ:270656184
defined('BASEPATH') || exit('No direct script access allowed');
class User extends MY_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->verify_user();
	}

	public function index()
	{
		$this->view('user');
	}

	public function data()
	{
		$result = $this->get_list('user');
		echo json_encode($result);
	}

	public function add()
	{
		$this->load->view('user_add');
	}

	public function insert()
	{
		$username = $this->input->post('username');
		$password = $this->input->post('password');
		$repassword = $this->input->post('repassword');
		$name = $this->input->post('name');
		$idno = $this->input->post('idno');
		$mobile = $this->input->post('mobile');
		$email = $this->input->post('email');
		$status = $this->input->post('status');
		$exist = $this->db->where('username', $username)->count_all_results('user');

		if ($exist) {
			ajax_return(1, '该帐号已被使用');
		}

		if ($password != $repassword) {
			ajax_return(1, '两次输入的密码不同');
		}

		$data = array('username' => $username, 'password' => $this->pwd->encrypt($password), 'name' => $name, 'idno' => $idno, 'mobile' => $mobile, 'email' => $email, 'status' => $status, 'add_time' => time());
		$rs = $this->db->insert('user', $data);

		if ($rs) {
			echo json_encode(array('msg' => '添加成功', 'error' => 0));
		}
		else {
			echo json_encode(array('msg' => '添加失败', 'error' => 1));
		}
	}

	public function edit($id)
	{
		$id = intval($id);
		$d = $this->db->where('id', $id)->get('user')->row_array();
		$this->load->view('user_edit', $d);
	}

	public function info($id)
	{
		$id = intval($id);
		$d = $this->db->where('id', $id)->get('user')->row_array();
		$this->load->view('user_info', $d);
	}

	public function update()
	{
		$id = $this->input->post('id');
		$status = $this->input->post('status');
		$data = array('status' => $status);
		$rs = $this->db->where('id', $id)->update('user', $data);

		if ($rs) {
			echo json_encode(array('msg' => '修改成功', 'error' => 0));
		}
		else {
			echo json_encode(array('msg' => '修改失败', 'error' => 1));
		}
	}

	public function del()
	{
		$id = $this->input->get('id');

		if ($id) {
			$rs = $this->db->where_in('id', $id)->delete('user');

			if ($rs) {
				echo json_encode(array('msg' => '删除成功', 'error' => 0));
			}
			else {
				echo json_encode(array('msg' => '删除失败', 'error' => 1));
			}
		}
		else {
			echo json_encode(array('msg' => '删除失败！至少选择一行数据', 'error' => 0));
		}
	}
}

?>
